package com.xiaoran.robot.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xiaoran.robot.controller.FriendMessageAdaptor;
import com.xiaoran.robot.controller.GroupMessageAdaptor;
import com.xiaoran.robot.entity.Msg;
import com.xiaoran.robot.entity.SenderInfo;

public class MessageParser {
    private final String activeCharCommand = YamlReader.instance.getValueByKey("mirai.activeCharCommand").toString();
    private final String activeChar = YamlReader.instance.getValueByKey("mirai.activeChar").toString();

    public JSONObject msg;

    public MessageParser(JSONObject msg){
        this.msg = msg;
    }


    /**
     * 找到合适的 adaptor处理消息
     */
    public void parseMessage(){
        System.out.println("进入parseMessage");
        SenderInfo senderInfo = senderInfo();
        if(senderInfo == null){
            return;
        }
        String msgType = senderInfo.getType();
        if("FriendMessage".equals(msgType)) {
            new FriendMessageAdaptor(msg()).handleMessage();
        } else if ("GroupMessage".equals(msgType)) {
            new GroupMessageAdaptor(msg()).handleMessage();
        } else{
            System.out.println("暂不支持后的消息类型");
        }
    }

    /**
     * 得到发送者信息
     * @return senderinfo
     */
    public SenderInfo senderInfo(){
        JSONObject data = JSON.parseObject(msg.getString("data"));
        String type = data.getString("type");
        if(type == null){
            return null;
        }
        JSONObject jsonObject = JSON.parseObject(data.getString("sender"));
        String qid = jsonObject.getString("id");
        String gid = "";
        if("GroupMessage".equals(type)){
            String group = jsonObject.getString("group");
            gid = StringUtils.toJson(group).getString("id");
        }
        return new SenderInfo(type, qid, gid);
    }

    /**
     * 解析消息
     * @return msg
     */
    public Msg msg(){
        JSONObject data = JSON.parseObject(msg.getString("data"));
        String messageChain = data.getString("messageChain");
        StringBuilder s = new StringBuilder();
        JSONArray msgcJson = JSONArray.parseArray(messageChain);
        for(Object item : msgcJson){
            JSONObject itemJson = StringUtils.toJson(item.toString());
            if("Plain".equals(itemJson.get("type"))) {
                s.append(itemJson.get("text"));
            }
        }
        String plainText = s.toString().trim();

        boolean cmdMsg = plainText.startsWith(activeCharCommand);
        boolean activeMsg = plainText.replace(activeCharCommand, "").startsWith(activeChar);
        int loopNum = 1;
        if(cmdMsg){
            String[] cmdS = plainText.split(" ");
            plainText = cmdS[0].replace(activeCharCommand, "");
             int loopNu = Integer.parseInt(cmdS[1]);
             loopNum = Math.min(loopNu, 5);
        }
        boolean activeDicts = false;
        if(activeMsg){
            plainText = plainText.replace(activeChar, "");
            activeDicts = new DictsParser(0).containsKey(plainText);
        }else{
            activeDicts = new DictsParser(1).containsKey(plainText);
        }

        boolean isAt = false;
        for(Object item : msgcJson) {
            JSONObject itemJson = StringUtils.toJson(item.toString());
            if("At".equals(itemJson.getString("type"))) {
                isAt = true;
                break;
            }
        }

        return new Msg(plainText
                , cmdMsg
                , activeMsg
                , senderInfo(), loopNum, activeDicts, isAt);
    }

}
